//展示框
const all = document.querySelector('.all')
//选项栏
const allChoose = document.querySelector('.allChoose')
// 信息选项
const chooseParent = document.querySelector('.chooseParent')

// 基本信息的DOM对象
const majorInformation = document.querySelector('.majorInformation')
const idNumber = document.querySelectorAll('.idNumber')
const nameInformation = document.querySelectorAll('.name')
const birthday = document.querySelector('.birthday')
const gender = document.querySelector('.gender')
const idCard = document.querySelector('.idCard')
const email = document.querySelector('.email')
const locationName = document.querySelector('.location')

// 学籍信息的DOM对象
const studyInformation = document.querySelector('.studyInformation')
const grade = document.querySelector('.grade')
const facultiesName = document.querySelector('.facultiesName')
const majorProgram = document.querySelector('.majorProgram')
const job = document.querySelector('.degree')

// 初始化
!function clearElseInformation() {
  while (majorInformation.nextElementSibling) {
    all.removeChild(majorInformation.nextElementSibling)
  }
}()

// 获取基本信息
function baseInformation() {
  axios({
    url: 'http://127.0.0.1:8080/Student/PersonData/Information',
    params: {
      id: localStorage.getItem('UID')
    }
  }).then(result => {
    // 获取后端结果集
    const resultInformation = result.data.data
    if (!resultInformation) {
      return
    }
    // 填充信息
    idNumber[0].innerHTML = idNumber[1].innerHTML = resultInformation.id
    nameInformation[0].innerHTML = nameInformation[1].innerHTML = resultInformation.name
    birthday.innerHTML = resultInformation.birthday
    gender.innerHTML = resultInformation.gender === '0' ? '女' : '男'
    idCard.innerHTML = resultInformation.idCard
    email.innerHTML = resultInformation.email
    locationName.innerHTML = resultInformation.location
  })
}

baseInformation()   //默认展示基本信息

// 获取学籍信息
function enrollmentInformation() {
  axios({
    url: 'http://127.0.0.1:8080/Student/PersonData/Information',
    params: {
      id: localStorage.getItem('UID')
    }
  }).then(async result => {
    // 获取后端结果集
    const resultInformation = result.data.data
    if (!resultInformation) {
      return
    }
    grade.innerHTML = (resultInformation.enrollmentDate).split('-')[0]
    // 获取院系名
    facultiesName.innerHTML = (await axios({
      url: 'http://127.0.0.1:8080/Student/PersonData/getFacultiesNameByFacultiesId',
      params: {
        id: resultInformation.facultiesId
      }
    })).data.data
    // 获取专业名
    majorProgram.innerHTML = (await axios({
      url: 'http://127.0.0.1:8080/Student/PersonData/getProfessionNameByProfessionId',
      params: {
        id: resultInformation.majorProgramId
      }
    })).data.data
    switch (resultInformation.degreeId) {
      case '1':
        job.innerHTML = '专科'
        break
      case '2':
        job.innerHTML = '本科'
        break
      case '3':
        job.innerHTML = '硕士研究生'
        break
      case '4':
        job.innerHTML = '博士研究生'
        break
      case '5':
        job.innerHTML = '博士后研究生'
        break
    }
  })
}

// 切换页面
chooseParent.addEventListener('click', (e) => {
  const element = e.target
  if (element.classList.contains('chooseParent')) {
    return
  }
  all.removeChild(allChoose.nextElementSibling)
  document.querySelector('.borderChoose').classList.remove('borderChoose')
  element.classList.add('borderChoose')
  if (element.classList.contains('majorInformationChoose')) {
    all.appendChild(majorInformation)
  } else if (element.classList.contains('studyInformationChoose')) {
    all.appendChild(studyInformation)
    enrollmentInformation()
  }
})